﻿using DimensionsHelper.Common.Contracts.Metadata;
using Style = DimensionsHelper.Common.Contracts.Metadata.Style;

namespace DimensionsHelper.Client.WinUi.ViewModels;

public partial class MetadataStyleViewModel : MetadataNamedViewModel
{
    public MetadataStyleViewModel() : base("style", MetadataNamedObjectKind.Style)
    {
    }

    private MetadataStyleViewModel(string name) : base(name, MetadataNamedObjectKind.Style)
    {
    }


    private void AddPropertyNullable<T>(string name, T? value)
    {
        if (value is null)
        {
            return;
        }

        MetadataPropertyViewModel property = new(name)
        {
            Value = value
        };

        AddChild(property);
    }


    public static MetadataStyleViewModel FromMetadata(Style style)
    {
        MetadataStyleViewModel styleViewModel = new();

        // properties
        styleViewModel.AddPropertyNullable(nameof(Style.Color), style.Color);
        styleViewModel.AddPropertyNullable(nameof(Style.BgColor), style.BgColor);
        styleViewModel.AddPropertyNullable(nameof(Style.IsHidden), style.IsHidden);
        styleViewModel.AddPropertyNullable(nameof(Style.Align), style.Align);
        styleViewModel.AddPropertyNullable(nameof(Style.ElementAlign), style.ElementAlign);
        styleViewModel.AddPropertyNullable(nameof(Style.VerticalAlign), style.VerticalAlign);
        styleViewModel.AddPropertyNullable(nameof(Style.Indent), style.Indent);
        styleViewModel.AddPropertyNullable(nameof(Style.ZIndex), style.ZIndex);
        styleViewModel.AddPropertyNullable(nameof(Style.Cursor), style.Cursor);
        styleViewModel.AddPropertyNullable(nameof(Style.Image), style.Image);
        styleViewModel.AddPropertyNullable(nameof(Style.ImagePosition), style.ImagePosition);
        styleViewModel.AddPropertyNullable(nameof(Style.Orientation), style.Orientation);
        styleViewModel.AddPropertyNullable(nameof(Style.Rows), style.Rows);
        styleViewModel.AddPropertyNullable(nameof(Style.Columns), style.Columns);
        styleViewModel.AddPropertyNullable(nameof(Style.Width), style.Width);
        styleViewModel.AddPropertyNullable(nameof(Style.Height), style.Height);

        // control
        MetadataStyleViewModel control = new("control");
        control.AddPropertyNullable(nameof(ControlStyle.Type), style.Control?.Type);
        control.AddPropertyNullable(nameof(ControlStyle.Accelerator), style.Control?.Accelerator);
        control.AddPropertyNullable(nameof(ControlStyle.ReadOnly), style.Control?.ReadOnly);
        styleViewModel.AddChild(control);

        // cell
        MetadataStyleViewModel cell = new("cell");
        cell.AddPropertyNullable(nameof(CellStyle.Height), style.Cell?.Height);
        cell.AddPropertyNullable(nameof(CellStyle.Width), style.Cell?.Width);
        cell.AddPropertyNullable(nameof(CellStyle.BorderColor), style.Cell?.BorderStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderTopColor), style.Cell?.BorderTopColor);
        cell.AddPropertyNullable(nameof(CellStyle.BorderBottomColor), style.Cell?.BorderBottomColor);
        cell.AddPropertyNullable(nameof(CellStyle.BorderLeftColor), style.Cell?.BorderLeftColor);
        cell.AddPropertyNullable(nameof(CellStyle.BorderRightColor), style.Cell?.BorderRightColor);
        cell.AddPropertyNullable(nameof(CellStyle.BorderStyle), style.Cell?.BorderStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderTopStyle), style.Cell?.BorderTopStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderBottomStyle), style.Cell?.BorderBottomStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderLeftStyle), style.Cell?.BorderLeftStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderRightStyle), style.Cell?.BorderRightStyle);
        cell.AddPropertyNullable(nameof(CellStyle.BorderWidth), style.Cell?.BorderWidth);
        cell.AddPropertyNullable(nameof(CellStyle.BorderTopWidth), style.Cell?.BorderTopWidth);
        cell.AddPropertyNullable(nameof(CellStyle.BorderBottomWidth), style.Cell?.BorderBottomWidth);
        cell.AddPropertyNullable(nameof(CellStyle.BorderLeftWidth), style.Cell?.BorderLeftWidth);
        cell.AddPropertyNullable(nameof(CellStyle.BorderRightWidth), style.Cell?.BorderRightWidth);
        cell.AddPropertyNullable(nameof(CellStyle.Padding), style.Cell?.Padding);
        cell.AddPropertyNullable(nameof(CellStyle.PaddingTop), style.Cell?.PaddingTop);
        cell.AddPropertyNullable(nameof(CellStyle.PaddingBottom), style.Cell?.PaddingBottom);
        cell.AddPropertyNullable(nameof(CellStyle.PaddingLeft), style.Cell?.PaddingLeft);
        cell.AddPropertyNullable(nameof(CellStyle.PaddingRight), style.Cell?.PaddingRight);
        cell.AddPropertyNullable(nameof(CellStyle.Wrap), style.Cell?.Wrap);
        cell.AddPropertyNullable(nameof(CellStyle.RowSpan), style.Cell?.RowSpan);
        cell.AddPropertyNullable(nameof(CellStyle.ColSpan), style.Cell?.ColSpan);
        cell.AddPropertyNullable(nameof(CellStyle.BgColor), style.Cell?.BgColor);
        cell.AddPropertyNullable(nameof(CellStyle.RepeatHeader), style.Cell?.RepeatHeader);
        cell.AddPropertyNullable(nameof(CellStyle.RepeatSideHeader), style.Cell?.RepeatSideHeader);
        styleViewModel.AddChild(cell);

        // font style
        MetadataStyleViewModel font = new("font");
        font.AddPropertyNullable(nameof(FontStyle.Family), style.Font?.Family);
        font.AddPropertyNullable(nameof(FontStyle.Size), style.Font?.Size);
        font.AddPropertyNullable(nameof(FontStyle.IsBold), style.Font?.IsBold);
        font.AddPropertyNullable(nameof(FontStyle.IsItalic), style.Font?.IsItalic);
        font.AddPropertyNullable(nameof(FontStyle.IsUnderline), style.Font?.IsUnderline);
        font.AddPropertyNullable(nameof(FontStyle.IsStrikethrough), style.Font?.IsStrikethrough);
        font.AddPropertyNullable(nameof(FontStyle.IsOverline), style.Font?.IsOverline);
        font.AddPropertyNullable(nameof(FontStyle.IsBlink), style.Font?.IsBlink);
        font.AddPropertyNullable(nameof(FontStyle.IsSuperscript), style.Font?.IsSuperscript);
        font.AddPropertyNullable(nameof(FontStyle.IsSubscript), style.Font?.IsSubscript);
        styleViewModel.AddChild(font);

        return styleViewModel;
    }
}